<?php
class Merchant_Model_Customer
{

	private $_table;
	//Initalizing Db Table
	public function __construct(){
		$this->_table = new Merchant_Model_DbTable_Customer();
	}

	public function getDbTable(){
		return $this->_table;
	}

	public static function getInstance(){
		return new Merchant_Model_Customer();
	}

	/**
	*add DB Adapter
	*/
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	/**
	*add One Row
	*@param array $row
	*@return boolean
	*/
	public function add($row){
		return $this->_table->insert($row);
	}
	/**
	*Update One Row
	*@param rowSet $row Customer's data
	*@param int $customer_id
	*@return boolean
	*/
	public  function update($row,$customerId){

		$where = $this->_table->getAdapter()->quoteInto('customer_id = ?', $customerId);

		return $this->_table->update($row, $where);

	}
	
	/**
	*  通过条件更新指定数据
	* @param array $row
	* @param array $cond
	* @return
	*/
	public function updateByVal($row,$field,$val){
		$where = $this->_table->getAdapter()->quoteInto("{$field} = ?", $val);
		return $this->_table->update($row, $where);
	}

	/**
	*Delete One Row
	*@param int $customer_id
	*@return boolean
	*/
	public function delete($customerId){

		$where = $this->_table->getAdapter()->quoteInto('customer_id = ?', $customerId);

		return $table->delete($where);

	}



	/**
	*Get Rowset By Name
	*@param string $customer_id 用户id
	*@return Rowset
	*/
	public function getByName($customerName){

		$select = $this->_table->select()->where('customer_id = ?', $customer_id);
		return $this->_table->fetchRow($select);

	}

	/**
	* 检测注册邮箱是否已经存在
	* @param string $email
	* @return boolean
	*/
	public function existsEmail($email){
		$select = $this->_table->select()->where('customer_email = ?', $email);
		$row = $this->_table->fetchRow($select);
		if($row){
			return $row;
		}
		return false;
	}

	/**
	* 通过email和password查找
	* @param string email
	* @param string password
	* @return zend_db_table_row
	*/
	public function getByEmailAndPassword($email,$password){
		$where = $this->_table->select()->where('customer_email = ? or customer_id = ?',$email)->where('customer_password = ?',$password);
		$row = $this->_table->fetchRow($where);
		return $row;
	}
	
	public function getByValue($value,$field='customer_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
    
    public function getByCustoemrIdAndActivateCode($customer_id,$customer_activate_code){
		$where = $this->_table->select()->where('customer_id = ?',$customer_id)->where('customer_activate_code = ?',$customer_activate_code);
		$row = $this->_table->fetchRow($where);
		return $row;
	}
	/*
	*判断是否是注册一个月内
	*/
	public function getByGwemCustomerId($customer_id){
		$select = $this->_table->select()->where('customer_id = ?', $customer_id)->where('date_add(customer_date_reg,interval 1 month)>now()');
		return $this->_table->fetchRow($select);
	}
}